package com.kylin.artwork.openapi.starter.datasource;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Optional;

/**
 * 汉薇Axon框架专用数据源
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月05日 11:35
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "kylinAxonEntityManagerFactory",
        transactionManagerRef = "kylinAxonTransactionManager")
@RequiredArgsConstructor
public class KylinAxonDataConfig {

    private final HibernateProperties hibernateProperties;
    private final JpaProperties jpaProperties;


    @Primary
    @Bean("kylinAxonDataSource")
    @Qualifier("kylinAxonDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.kylin-axon")
    public DataSource kylinAxonDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Primary
    @Bean("kylinAxonEntityManager")
    public EntityManager kylinAxonEntityManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(kylinAxonEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return Optional.ofNullable(factory).orElseThrow(NullPointerException::new).createEntityManager();
    }


    @Primary
    @Bean("kylinAxonEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean kylinAxonEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(kylinAxonDataSource())
                .packages("org.axonframework.eventsourcing.eventstore.jpa",
                        "org.axonframework.modelling.saga.repository.jpa",
                        "org.axonframework.eventhandling.tokenstore.jpa")
                .properties(hibernateProperties.determineHibernateProperties(
                        jpaProperties.getProperties(), new HibernateSettings()))
                .persistenceUnit("kylinAxonPersistenceUnit").build();
    }


    @Primary
    @Bean("kylinAxonTransactionManager")
    public PlatformTransactionManager kylinAxonTransactionManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(kylinAxonEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return new JpaTransactionManager(
                Optional.ofNullable(factory).orElseThrow(NullPointerException::new));

    }
}